package vip.meeet.network.http;

import com.alibaba.fastjson.JSON;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;

public class TrustAnyTrustManager implements X509TrustManager {

    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[]{};
    }

    public static void main(String[] args) throws IOException, KeyManagementException, NoSuchAlgorithmException {
//        test2();
        test3();
    }

    public static void test() throws NoSuchAlgorithmException, IOException, KeyManagementException {
        URL url = new URL("https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=hgnd&rowcode=zb&colcode=sj&wds=[]&dfwds=[{\"wdcode\":\"sj\",\"valuecode\":\"LAST20\"}]&k1=1601005045577");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
        conn.setSSLSocketFactory(sc.getSocketFactory());
        InputStream inputStream = conn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }

    public static void test2() throws IOException, NoSuchAlgorithmException, KeyManagementException {
        URL url = new URL("https://data.stats.gov.cn/easyquery.htm");
        HttpsURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection();
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
        httpURLConnection.setSSLSocketFactory(sc.getSocketFactory());

        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setDoOutput(true);


        httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
        httpURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
        httpURLConnection.setRequestProperty("Connection", "keep-alive");
        httpURLConnection.setRequestProperty("Host", "data.stats.gov.cn");
        httpURLConnection.setRequestProperty("Referer", "http://data.stats.gov.cn/easyquery.htm?cn=C01");
        httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");
        httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
//        httpURLConnection.setRequestProperty("Cookie", "JSESSIONID=3C2301BD97EEFADDFAA46D502709E769; u=1; experience=show");

        HashMap<Object, Object> map = new HashMap<>();
        map.put("m", "QueryData");
        map.put("dbcode", "hgnd");
        map.put("rowcode", "zb");
        map.put("colcode", "sj");
        map.put("wds", "");
        map.put("dfwds", "[{\"wdcode\":\"sj\",\"valuecode\":\"LAST20\"}]");
        map.put("k1", String.valueOf(System.currentTimeMillis()));

        byte[] params = JSON.toJSONBytes(map);

        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(params);
        outputStream.flush();
        outputStream.close();
        InputStream inputStream = httpURLConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }

    public static void test3() throws IOException, NoSuchAlgorithmException, KeyManagementException {
        String urlBase = "https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=hgnd&rowcode=zb&colcode=sj&wds=[]&dfwds=[{\"wdcode\":\"sj\",\"valuecode\":\"LAST20\"}]&k1=%d";
        URL url = new URL(String.format(urlBase,System.currentTimeMillis()));
        HttpsURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection();
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
        httpURLConnection.setSSLSocketFactory(sc.getSocketFactory());

        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setDoOutput(true);


        httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
        httpURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
        httpURLConnection.setRequestProperty("Connection", "keep-alive");
        httpURLConnection.setRequestProperty("Host", "data.stats.gov.cn");
        httpURLConnection.setRequestProperty("Referer", "https://data.stats.gov.cn/easyquery.htm?cn=C01&zb=A0201&sj=2019");
        httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");
        httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        httpURLConnection.setRequestProperty("Cookie", "_trs_uv=k9kuv0f6_6_jjij; JSESSIONID=Tm7DJCUMv2k_rDFw5PBdt4j5t4cISNJjxkeh60sMPwQ5etQJJsHu!-325436833; u=6");

        InputStream inputStream = httpURLConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

    }
}